layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_delivery_box_detail_for_edit';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段，菜单ID**/
			t.c.pcode = c.pcode;
		},
        load:function () { // 加载控件
        	var h = ``
        		+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `">`
    				+ `<label style="font-weight: 700;font-size: 18px; padding: 0 15px;">装箱详情</label>`
        		+ `</div>`
            	+ `<div class="layui-form layui-form-item" pane>`
            		+ `<div style="margin-left: 10px;" id="`+this.c.tableId+`">`
            			+ this.loadTable()
            		+ `</div>`
        		+ `</div>`
    		+ `</div>`;
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return ``
        	+ `<div class="layui-table-box">`
        		+ `<div class="layui-table-header">`
	            	+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="">`
	            		+ `<thead>`
	            			+ this.loadTableOfThead()
	            		+ `</thead>`
	            		+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
	        		+ `</table>`
        		+ `</div>`
    		+ `</div>`;
        },
        loadTableOfThead:function () { // 加载表头
            var h = `<thead>`;
            h +=`<tr>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">商品编码</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">商品名称</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">品名</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">商品材质</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">海关编码</th>`;
            h += `<th style="width:4%; text-align: center; color: #000000;">箱号</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">箱内数量</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">箱子长度(cm)</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">箱子宽度(cm)</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">箱子高度(cm)</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">箱重(kg)</th>`;
            h += `<th style="width:6%; text-align: center; color: #000000;">抛重</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">唛头</th>`;
            h += `</tr>`;
            h + `</thead>`;
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = `<tbody>`;
            for(var i=0;i<t.c.data.length;i++){
	            h += `<tr>`;
	            h += `<td style="text-align:center; padding: 0; margin:1px;">` + t.c.data[i].productCode + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].productName + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].productNameCn + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].productMaterialCn + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].productCodeHs + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].boxNo + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].productNum + `</td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" class="layui-input" data-index="` + i + `" data-type="sizeLength" lay-filter="_input"  value="` + t.c.data[i].sizeLength + `" ></td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" class="layui-input" data-index="` + i + `" data-type="sizeWidth" lay-filter="_input"  value="` + t.c.data[i].sizeWidth + `" ></td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" class="layui-input" data-index="` + i + `" data-type="sizeHeight" lay-filter="_input"  value="` + t.c.data[i].sizeHeight + `" ></td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" class="layui-input" data-index="` + i + `" data-type="sizeWeight" lay-filter="_input"  value="` + t.c.data[i].sizeWeight + `" ></td>`;
	            h += `<td style="text-align:center; padding: 0;" data-index="` + i + `" data-type="weightVol">` + t.c.data[i].weightVol + `</td>`;
	            h += `<td style="text-align:center; padding: 2px;"><input type="text" class="layui-input" data-index="` + i + `" data-type="remark" lay-filter="_input"  value="` + t.c.data[i].remark + `" ></td>`;
                h+= `</tr>`;
            }
            
            h += `</tbody>`;
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
            
            var domTable = $('#' + t.c.tableId);
			// 监听表格中各个输入框
            domTable.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				
				// 若改变的是长宽高，则重新计算各个值
				if (key == 'sizeLength' || key == 'sizeWidth' || key == 'sizeHeight') {
					var weightVol = t.reCalWeightVolSingle(index);
				} else if (key == 'sizeWeight') {
					var weightVol = t.calFormDataAfterChange();
				}
				return;
			});
            
            // 表单控件重新加载
        	form.render();
        },
        reCalWeightVolSingle:function (index) { // 重新计算单条抛重，抛重=长(cm)*宽(cm)*高(cm)/6000
        	console.log('---reCalWeightVolSingle---');
        	var t = this;
        	var row = t.c.data[index];
            var weightVol = parseInt(row['sizeLength']) * parseInt(row['sizeWidth']) * parseInt(row['sizeHeight']) / 6000;
            weightVol = weightVol.toFixed(2);
            
            // 给单条抛重赋值
            t.c.data[index].weightVol = weightVol;
            $('#' + t.c.tableId).find('td[data-type="weightVol"][data-index="' + index + '"]').html(weightVol);
            
            return weightVol;
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        reloadData:function () {
        	var t = this;
			$.ajax({
				type: "POST",
				url: '/hr/choiceshop/detailData',
				data: {pcode: t.c.pcode},
				success: function (res) {
					if (res.code == 0) {
						t.c.data = res.data;
						t.reload(t.c);
					} else {
						layer.msg('加载失败，请刷新页面');
					}
				}
			});
        },
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_delivery_box_detail_for_edit', object);
});